#include "hal_ledboard.h"
#include "hk32c030xx_gpio.h"

static floor_led_node_t keyboard[6] = {
	{LED_FLOOR1_BASE,LED_FLOOR1_PIN},
	{LED_FLOOR1_BASE,LED_FLOOR2_PIN},
	{LED_FLOOR1_BASE,LED_FLOOR3_PIN},
	{LED_FLOOR2_BASE,LED_FLOOR4_PIN},
	{LED_FLOOR2_BASE,LED_FLOOR5_PIN},
	{LED_FLOOR2_BASE,LED_FLOOR6_PIN},
};

static void floor_ledboard_init() {
	GPIO_InitTypeDef ledboard_;
	RCC_AHBPeriphClockCmd(LED_FLOOR1_RCU | LED_FLOOR2_RCU, ENABLE);
	
    ledboard_.GPIO_Pin = LED_FLOOR1_PIN | LED_FLOOR2_PIN | LED_FLOOR3_PIN;
    ledboard_.GPIO_Mode = GPIO_Mode_OUT;
    ledboard_.GPIO_OType = GPIO_OType_PP;
    ledboard_.GPIO_Speed = GPIO_Speed_50MHz;
    ledboard_.GPIO_PuPd = GPIO_PuPd_NOPULL;
    ledboard_.GPIO_Schmit = GPIO_Schmit_Disable;
	
    GPIO_Init(LED_FLOOR1_BASE, &ledboard_);
	
	ledboard_.GPIO_Pin = LED_FLOOR4_PIN | LED_FLOOR5_PIN | LED_FLOOR6_PIN;

	GPIO_Init(LED_FLOOR2_BASE, &ledboard_);
}

static void floor_ctrl_led(uint8_t floor,uint8_t flag) {
	if(floor >= 6)
		return ;
	GPIO_WriteBit(keyboard[floor].floor_base,keyboard[floor].floor_pin,flag);
}

static void floor_led_toggle(uint8_t floor) {
	for(int i = 0; i < 6;i++) {
		if(floor & (1 << i)) {
			GPIO_Toggle(keyboard[i].floor_base,keyboard[i].floor_pin);
		}
	}
}

static void floor_led_open(uint8_t table) {

	for(int i = 0; i < 6;i++) {
		if(table & (1 << i)) {
			GPIO_WriteBit(keyboard[i].floor_base,keyboard[i].floor_pin,1);	
		} else {
			GPIO_WriteBit(keyboard[i].floor_base,keyboard[i].floor_pin,0);		
		}
	}
}

 const ledboard_floor_t g_floor_led_ = {
	floor_ledboard_init,
	floor_ctrl_led,
	floor_led_toggle,
	floor_led_open,
 };
